package com.mate.cloud.config;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

/**
 * 配置文文件加载抽象类
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2025/1/28 11:42
 * @updateUser: MI
 * @updateTime: 2025/1/28 11:42
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public abstract class AbstractPropertyLoader {

    // 抽象方法，子类提供具体的文件路径
    protected abstract String getFilePath();

    // 加载并打印属性文件内容
    public Properties loadAndPrintProperties() {
        String filePath = getFilePath();
        Resource resource = new ClassPathResource(filePath);

        try {
            // 加载 properties 文件
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);

            // 打印 properties 内容
            properties.list(System.out);
            System.out.println("==============================================");

            return properties;
        } catch (IOException e) {
            throw new RuntimeException("Failed to load properties file:" + filePath, e);
        }
    }
}
